
#### Replication Code for Shelby Grossman, "The Politics of Order in Informal Markets: Evidence from Lagos", 2020. shelbygrossman@gmail.com

#####
#####
#####INTRO CODE
#####
#####

rm(list=ls())

install.packages("stargazer")
install.packages("dplyr")
library(stargazer)
library(dplyr)

#set working directory 
setwd("~/Dropbox/Book/Book manuscript/WorldPolitics/analysis/replication")

#load data
lts1_trader <- read.csv("replication_data.csv")

#####
#####CREATE PRODUCT FRACTIONALIZATION INDEX
#####

data2 <- ddply(lts1_trader, ~associationID, summarise, count_apparel = sum(productaltcat_apparel), count_electronics = sum(productaltcat_electronics), count_beauty = sum(productaltcat_beauty), count_hardware = sum(productaltcat_hardware), count_home = sum(productaltcat_home))

data2$product_total <- data2$count_apparel + data2$count_electronics + data2$count_beauty + data2$count_hardware + data2$count_home

data2$apparel_prop <- data2$count_apparel/data2$product_total
data2$electronics_prop <- data2$count_electronics/data2$product_total
data2$beauty_prop <- data2$count_beauty/data2$product_total
data2$hardware_prop <- data2$count_hardware/data2$product_total
data2$home_prop <- data2$count_home/data2$product_total

data2$prodfrac <- 1 - ((data2$apparel_prop)^2 + (data2$electronics_prop)^2  + (data2$beauty_prop)^2  + (data2$hardware_prop)^2 + (data2$home_prop)^2)

lts1_trader <- merge(lts1_trader, data2, by.x="associationID", by.y="associationID", all=TRUE)

#####
#####MAKE SOME VARIABLES NUMERIC
#####

#make leader life variable numeric
lts1_trader$leader_life_numeric <- lts1_trader$leader_life
levels(lts1_trader$leader_life_numeric ) <- c(levels(lts1_trader$leader_life_numeric ), 0,1)
lts1_trader$leader_life_numeric[lts1_trader$leader_life_numeric  == "Refuse to answer"] <- NA
lts1_trader$leader_life_numeric[lts1_trader$leader_life_numeric  == "Yes"] <- 1
lts1_trader$leader_life_numeric[lts1_trader$leader_life_numeric == "No"] <- 0
lts1_trader$leader_life_numeric <- as.numeric(as.character(lts1_trader$leader_life_numeric))

#make represent variable numeric
lts1_trader$f11_plazarepresent_numeric <- lts1_trader$f11_plazarepresent
levels(lts1_trader$f11_plazarepresent_numeric ) <- c(levels(lts1_trader$f11_plazarepresent_numeric ), 0,.5,1)
lts1_trader$f11_plazarepresent_numeric[lts1_trader$f11_plazarepresent_numeric  == "Don't know"] <- NA
lts1_trader$f11_plazarepresent_numeric[lts1_trader$f11_plazarepresent_numeric  == "Yes"] <- 1
lts1_trader$f11_plazarepresent_numeric[lts1_trader$f11_plazarepresent_numeric  == "Sometimes"] <- 0.5
lts1_trader$f11_plazarepresent_numeric[lts1_trader$f11_plazarepresent_numeric == "No"] <- 0
lts1_trader$f11_plazarepresent_numeric <- as.numeric(as.character(lts1_trader$f11_plazarepresent_numeric))

#make honest account variable numeric
lts1_trader$f14_honestaccount_numeric <- lts1_trader$f14_honestaccount
levels(lts1_trader$f14_honestaccount_numeric ) <- c(levels(lts1_trader$f14_honestaccount_numeric ), 0,1)
lts1_trader$f14_honestaccount_numeric[lts1_trader$f14_honestaccount_numeric  == "Don't know"] <- NA
lts1_trader$f14_honestaccount_numeric[lts1_trader$f14_honestaccount_numeric  == "Yes"] <- 1
lts1_trader$f14_honestaccount_numeric[lts1_trader$f14_honestaccount_numeric == "No"] <- 0
lts1_trader$f14_honestaccount_numeric <- as.numeric(as.character(lts1_trader$f14_honestaccount_numeric))

#make free complain variable numeric
lts1_trader$f13_freecomplain_numeric <- lts1_trader$f13_freecomplain
levels(lts1_trader$f13_freecomplain_numeric ) <- c(levels(lts1_trader$f13_freecomplain_numeric ), 0,1)
lts1_trader$f13_freecomplain_numeric[lts1_trader$f13_freecomplain_numeric  == "Don't know"] <- NA
lts1_trader$f13_freecomplain_numeric[lts1_trader$f13_freecomplain_numeric  == "Yes"] <- 1
lts1_trader$f13_freecomplain_numeric[lts1_trader$f13_freecomplain_numeric == "No"] <- 0
lts1_trader$f13_freecomplain_numeric <- as.numeric(as.character(lts1_trader$f13_freecomplain_numeric))

#make disputes variable numeric
lts1_trader$f8_traderdisputes_numeric <- lts1_trader$f8_traderdisputes
levels(lts1_trader$f8_traderdisputes_numeric ) <- c(levels(lts1_trader$f8_traderdisputes_numeric ), 0,1)
lts1_trader$f8_traderdisputes_numeric[lts1_trader$f8_traderdisputes_numeric  == "refuse"] <- NA
lts1_trader$f8_traderdisputes_numeric[lts1_trader$f8_traderdisputes_numeric  == "Yes"] <- 1
lts1_trader$f8_traderdisputes_numeric[lts1_trader$f8_traderdisputes_numeric == "No"] <- 0
lts1_trader$f8_traderdisputes_numeric <- as.numeric(as.character(lts1_trader$f8_traderdisputes_numeric))

#make election information variable numeric
lts1_trader$e7_electinfo_numeric <- lts1_trader$e7_electinfo
levels(lts1_trader$e7_electinfo_numeric) <- c(levels(lts1_trader$e7_electinfo_numeric), 0,1)
lts1_trader$e7_electinfo_numeric[lts1_trader$e7_electinfo_numeric =="Don't know"] <- NA
lts1_trader$e7_electinfo_numeric[lts1_trader$e7_electinfo_numeric =="Yes"] <- 1
lts1_trader$e7_electinfo_numeric[lts1_trader$e7_electinfo_numeric =="No"] <- 0
lts1_trader$e7_electinfo_numeric <- as.numeric(as.character(lts1_trader$e7_electinfo_numeric))

#make sleep variable numeric
lts1_trader$f16_sleep_numeric <- lts1_trader$f16_sleep
levels(lts1_trader$f16_sleep_numeric) <- c(levels(lts1_trader$f16_sleep_numeric), 0,1)
lts1_trader$f16_sleep_numeric[lts1_trader$f16_sleep_numeric =="Don't know"] <- NA
lts1_trader$f16_sleep_numeric[lts1_trader$f16_sleep_numeric =="Yes"] <- 1
lts1_trader$f16_sleep_numeric[lts1_trader$f16_sleep_numeric =="No"] <- 0
lts1_trader$f16_sleep_numeric <- as.numeric(as.character(lts1_trader$f16_sleep_numeric))

#####
##### REMOVE TRADERS WITHOUT ASSOCIATIONS AND TRADERS IN MARKETS ON FEDERAL GOVERNMENT LAND (SEE PAPER FOR EXPLANATION)
#####

#Association ID 201 is NA, i.e. traders who did not provide their association. Here I remove these respondents.
lts1_trader_x <- lts1_trader
lts1_trader_x$associationID[lts1_trader_x$associationID ==201] <- NA
lts1_trader_x <- lts1_trader_x[!(is.na(lts1_trader_x$associationID)),] #175 NA's

#the first two observations are also respondents who refused to provide their associations
lts1_trader_x = lts1_trader_x[-1,]
lts1_trader_x = lts1_trader_x[-1,]

lts1_trader <- lts1_trader_x #N = 1004
stopifnot( nrow(lts1_trader) == 1004)
                 
lts1_trader <- subset(lts1_trader, lts1_trader$land !="Federal") 
stopifnot( nrow(lts1_trader) == 729)

#####
#####FLIP ORDER FOR FACTORS IN THE LAND TYPE VARIABLE
#####

lts1_trader$land <-factor(lts1_trader$land, levels = c("Private land", "Local government land"))

#####
#####SUBSET TO TRADERS IN ASSOCIATIONS WHERE AT LEAST 5 TRADERS ARE REPRESENTED FOR EACH ASSOCIATION
#####

lts1_trader_biggerassoc <- subset(lts1_trader, number_traders_rep_assoc > 4)

#####
#####
#####END INTRO CODE
#####
#####

#####
#####
##### TABLE 1: DESCRIPTIVE STATISTICS
#####
#####

lts1_trader[57, "realannualrent_usd"] <- NA

lts1_trader$g4a1_stockvalue_USD <- lts1_trader$g4a1_stockvalue/199 #converting stock value to USD (199 was the exchange rate at the time of the survey)

variables_for_summ_stats <- c("f11_plazarepresent_numeric", "f14_honestaccount_numeric", "f13_freecomplain_numeric",  "f8_traderdisputes_numeric",   "postsecondary",  "male", "b6_employees", "realannualrent_usd", "g4a1_stockvalue_USD","productaltcat_apparel","productaltcat_electronics","productaltcat_beauty","productaltcat_hardware","productaltcat_home", "leader_life_numeric", "e7_electinfo_numeric", "f16_sleep_numeric")
variables_for_summ_stats_df <- lts1_trader[variables_for_summ_stats]

stargazer(variables_for_summ_stats_df , summary=TRUE, covariate.labels=c("Represent","Honest account","Free complain","Resolve disputes","Post-sec. education","Men","Number employees","Annual rent (USD)","Stock value (USD)","Sell apparel","Sell electronics","Sell beauty","Sell hardware","Sell home goods","Leader for life","Leader provides political info","Sleep election eve"), 
		table.placement="H", 
		digits=2, 
		no.space=TRUE,
		omit.stat=c("ser","f"),
		font.size="footnotesize")

public <- subset(lts1_trader, lts1_trader$land=="Local government land")

private <- subset(lts1_trader, lts1_trader$land=="Private land")

t.test(public$f11_plazarepresent_numeric, private$f11_plazarepresent_numeric)
t.test(public$f14_honestaccount_numeric, private$f14_honestaccount_numeric)
t.test(public$f13_freecomplain_numeric, private$f13_freecomplain_numeric)
t.test(public$f8_traderdisputes_numeric, private$f8_traderdisputes_numeric)
t.test(public$postsecondary, private$postsecondary)
t.test(public$male, private$male)
t.test(public$b6_employees, private$b6_employees)
t.test(public$realannualrent_usd, private$realannualrent_usd)
t.test(public$g4a1_stockvalue_USD, private$g4a1_stockvalue_USD)
t.test(public$productaltcat_apparel, private$productaltcat_apparel)
t.test(public$productaltcat_electronics, private$productaltcat_electronics)
t.test(public$productaltcat_beauty, private$productaltcat_beauty)
t.test(public$productaltcat_hardware, private$productaltcat_hardware)
t.test(public$productaltcat_home, private$productaltcat_home)
t.test(public$leader_life_numeric, private$leader_life_numeric)
t.test(public$e7_electinfo_numeric, private$e7_electinfo_numeric)
t.test(public$f16_sleep_numeric, private$f16_sleep_numeric)

#####
#####
##### TABLE 2
#####
#####

#represent
reg1_rep <- lm(f11_plazarepresent_numeric  ~ land, data=lts1_trader)
reg1_rep$clse <-cluster.vcov(reg1_rep, lts1_trader$associationID) 
reg1_rep_cluster <- coeftest(reg1_rep, reg1_rep$clse)

reg1_rep_life <- lm(f11_plazarepresent_numeric  ~ land + leader_life_numeric + local_govt +postsecondary  + b6_employees + log(g4a1_stockvalue), data=lts1_trader)
reg1_rep_life$clse <-cluster.vcov(reg1_rep_life, lts1_trader$associationID) 
reg1_rep_life_cluster <- coeftest(reg1_rep_life, reg1_rep_life$clse)

#honest accounting
reg2_honest <- lm(f14_honestaccount_numeric ~ land, data=lts1_trader)
reg2_honest$clse <-cluster.vcov(reg2_honest, lts1_trader$associationID) 
reg2_honest_cluster <- coeftest(reg2_honest, reg2_honest$clse)

reg2_honest_life <- lm(f14_honestaccount_numeric  ~ land + leader_life_numeric + local_govt + postsecondary  + b6_employees + log(g4a1_stockvalue), data=lts1_trader)
reg2_honest_life$clse <-cluster.vcov(reg2_honest_life, lts1_trader$associationID) 
reg2_honest_life_cluster <- coeftest(reg2_honest_life, reg2_honest_life$clse)

#free complain
reg3_complain <- lm(f13_freecomplain_numeric  ~ land, data=lts1_trader)
reg3_complain$clse <-cluster.vcov(reg3_complain, lts1_trader$associationID) 
reg3_complain_cluster <- coeftest(reg3_complain, reg3_complain$clse)

reg3_complain_life <- lm(f13_freecomplain_numeric ~ land + leader_life_numeric + local_govt + postsecondary  + b6_employees + log(g4a1_stockvalue), data=lts1_trader)
reg3_complain_life$clse <-cluster.vcov(reg3_complain_life, lts1_trader$associationID) 
reg3_complain_life_cluster <- coeftest(reg3_complain_life, reg3_complain_life$clse)

#disputes
reg4_disputes <- lm(f8_traderdisputes_numeric ~ land, data=lts1_trader)
reg4_disputes$clse <-cluster.vcov(reg4_disputes, lts1_trader$associationID) 
reg4_disputes_cluster <- coeftest(reg4_disputes, reg4_disputes$clse)

reg4_disputes_life <- lm(f8_traderdisputes_numeric ~ land + leader_life_numeric + local_govt + postsecondary  + b6_employees + log(g4a1_stockvalue), data=lts1_trader)
reg4_disputes_life$clse <-cluster.vcov(reg4_disputes_life, lts1_trader$associationID) 
reg4_disputes_life_cluster <- coeftest(reg4_disputes_life, reg4_disputes_life$clse)

stargazer(reg1_rep_cluster, reg1_rep_life_cluster, reg2_honest_cluster, 	reg2_honest_life_cluster, reg3_complain_cluster, reg3_complain_life_cluster, reg4_disputes_cluster, reg4_disputes_life_cluster, type = "text",
column.labels   = c("Represent","Honest accounting","Free to complain","Resolve dispute"), column.separate = c(2,2,2,2), covariate.labels=c("Local government land"), title="This table shows the relationship between land type and private good governance. The sample size changes across models due to missing data. Controls include whether the market leader holds her position for life, whether the trader has any post-secondary education, the number of employees the trader has, and the value of stock in a trader's shop. LGA fixed effects indicate the inclusion of local government fixed effects. Standard errors are clustered at the market association level.", table.placement="H", digits=2, no.space=TRUE, omit.stat=c("ser","f"), font.size="footnotesize",
omit=c("local_govt","leader_life_numeric","postsecondary","b6_employees","g4a1_stockvalue"), add.lines = list(c("Observations","670","640","519","498","674","642","699","664"),c("Controls","No","Yes","No","Yes","No", "Yes","No","Yes"),c("LGA fixed effects", "No","Yes","No","Yes","No", "Yes","No","Yes")))

#####
#####
##### TABLE 3
#####
#####

lts1_trader_biggerassoc$land_numeric <- as.numeric(as.factor(lts1_trader_biggerassoc$land))

reg1_rep <- lm(f11_plazarepresent_numeric  ~ land + prodfrac + land*prodfrac, data=lts1_trader_biggerassoc)
reg1_rep$clse <-cluster.vcov(reg1_rep, lts1_trader_biggerassoc$associationID) 
reg1_rep_cluster <- coeftest(reg1_rep, reg1_rep$clse)

reg2_honest <- lm(f14_honestaccount_numeric  ~ land + prodfrac + land*prodfrac, data=lts1_trader_biggerassoc)
reg2_honest$clse <-cluster.vcov(reg2_honest, lts1_trader_biggerassoc$associationID) 
reg2_honest_cluster <- coeftest(reg2_honest, reg2_honest$clse)

reg3_complain <- lm(f13_freecomplain_numeric ~ land + prodfrac + land*prodfrac, data=lts1_trader_biggerassoc)
reg3_complain$clse <-cluster.vcov(reg3_complain, lts1_trader_biggerassoc$associationID) 
reg3_complain_cluster <- coeftest(reg3_complain, reg3_complain$clse)

reg4_disputes <- lm(f8_traderdisputes_numeric  ~ land + prodfrac + land*prodfrac, data=lts1_trader_biggerassoc)
reg4_disputes$clse <-cluster.vcov(reg4_disputes, lts1_trader_biggerassoc$associationID) 
reg4_disputes_cluster <- coeftest(reg4_disputes, reg4_disputes$clse)

stargazer(reg1_rep_cluster, reg2_honest_cluster, reg3_complain_cluster, reg4_disputes_cluster, column.labels   = c("Represent","Honest accounting","Free to complain","Resolve dispute"), column.separate = c(1,1,1,1),
covariate.labels=c("Local government land","Product diversification","Local govt. land*product diversification"), title="This table shows the interaction of land type and a market's product diversification index. A higher diversification index indicates that the market has traders selling a wider variety of products. The data for this table is subsetted to markets where there are five or more traders surveyed in the association. Standard errors are clustered at the market association level.", table.placement="H", digits=2, no.space=TRUE, omit.stat=c("ser","f"), font.size="footnotesize", add.lines = list(c("Observations","477","359","472","494"), c("Controls","No","No","No","No"), c("LGA fixed effects", "No","No","No","No")))

#####
#####
##### TABLE 4
#####
#####

#represent
rep_info <- lm(f11_plazarepresent_numeric  ~ e7_electinfo_numeric, data=lts1_trader)
rep_info$clse <-cluster.vcov(rep_info, lts1_trader$associationID) 
rep_info_cluster <- coeftest(rep_info, rep_info$clse)

rep_sleep <- lm(f11_plazarepresent_numeric  ~ f16_sleep_numeric, data=lts1_trader)
rep_sleep$clse <-cluster.vcov(rep_sleep, lts1_trader$associationID) 
rep_sleep_cluster <- coeftest(rep_sleep, rep_sleep$clse)

#honest accounting
honest_info <- lm(f14_honestaccount_numeric  ~ e7_electinfo_numeric, data=lts1_trader)
honest_info$clse <-cluster.vcov(honest_info, lts1_trader$associationID) 
honest_info_cluster <- coeftest(honest_info, honest_info$clse)

honest_sleep <- lm(f14_honestaccount_numeric  ~ f16_sleep_numeric, data=lts1_trader)
honest_sleep $clse <-cluster.vcov(honest_sleep, lts1_trader$associationID) 
honest_sleep_cluster <- coeftest(honest_sleep, honest_sleep $clse)

#free complain
complain_info <- lm(f13_freecomplain_numeric ~ e7_electinfo_numeric, data=lts1_trader)
complain_info$clse <-cluster.vcov(complain_info, lts1_trader$associationID) 
complain_info_cluster <- coeftest(complain_info, complain_info$clse)

complain_sleep <- lm(f13_freecomplain_numeric  ~ f16_sleep_numeric, data=lts1_trader)
complain_sleep$clse <-cluster.vcov(complain_sleep, lts1_trader$associationID) 
complain_sleep_cluster <- coeftest(complain_sleep, complain_sleep$clse)

#disputes
dispute_info <- lm(f8_traderdisputes_numeric  ~ e7_electinfo_numeric, data=lts1_trader)
dispute_info$clse <-cluster.vcov(dispute_info, lts1_trader$associationID) 
dispute_info_cluster <- coeftest(dispute_info, dispute_info$clse)

dispute_sleep <- lm(f8_traderdisputes_numeric  ~ f16_sleep_numeric, data=lts1_trader)
dispute_sleep$clse <-cluster.vcov(dispute_sleep, lts1_trader$associationID) 
dispute_sleep_cluster <- coeftest(dispute_sleep, dispute_sleep$clse)

stargazer(rep_info_cluster,rep_sleep_cluster,honest_info_cluster,honest_sleep_cluster,complain_info_cluster,complain_sleep_cluster,dispute_info_cluster,dispute_sleep_cluster, column.labels   = c("Represent","Honest accounting","Free to complain","Resolve dispute"), column.separate = c(2,2,2,2), covariate.labels=c("Election info","Sleep"), font.size="footnotesize", title="This table shows the relationship between different measures of political engagement and market leader good governance. Standard errors are clustered at the market association level.", no.space=TRUE, digits=2, omit.stat=c("ser","f"), add.lines = list(c("Observations","614","542","488","432","625","547","639","562"), c("Controls","No","No","No","No","No","No","No","No"), c("LGA fixed effects","No","No","No","No","No","No","No","No")))
